<template>
  <div class="detail-page" v-if="item.id">
    <van-nav-bar left-text="返回" @click-left="$router.back()" fixed title="面经详情" />
    <header class="header">
      <h1>{{ item.stem }}</h1>

      <p>
        {{ item.createdAt }} | {{ item.views }} 浏览量 | {{ item.likeCount }} 点赞数
      </p>

      <p>
        <img :src="item.avatar" alt="" />
        <span>{{ item.creator }}</span>

      </p>

    </header>

    <main class="body" v-html="item.content">

    </main>

    <div class="opt">
      <van-icon :class="{active:item.likeFlag}" name="like-o" @click="likeAndCollect(1)" />
      <van-icon :class="{ active: item.collectFlag }" name="star-o" @click="likeAndCollect(2)" />
    </div>

  </div>

  <div v-else><van-skeleton title avatar :row="3" style="margin-top: 40px;" />
    <van-skeleton title :row="3" style="margin-top: 40px;" />
    <van-skeleton title :row="3" style="margin-top: 40px;" />
  </div>
</template>

<script>
import { getAtricDetial, opt } from '@/api/user'

export default {
  name: 'DetailPage',
  data () {
    return {
      item: {}
    }
  },
  async created () {
    const id = this.$route.params.id
    const res = await getAtricDetial(id)
    this.item = res.data
  },
  methods: {
    async likeAndCollect (num) {
      await opt({ id: this.$route.params.id, optType: num })
      if (num === 1) {
        this.item.likeFlag = !this.item.likeFlag
        if (this.item.likeFlag) {
          this.item.likeCount++
          this.$toast('点赞成功')
        } else {
          this.item.likeCount--
          this.$toast('取消点赞')
        }
      } else {
        this.item.collectFlag = !this.item.collectFlag
        if (this.item.collectFlag) {
          this.$toast('收藏成功')
        } else {
          this.$toast('取消收藏')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;

  .header {
    h1 {
      font-size: 24px;
    }

    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }

    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }

  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;

    >.van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;

      &.active {
        background: #FEC635;
        color: #fff;
      }
    }
  }
}
</style>
